﻿; (function () {

    'use strict';

    window.CellModel = function CellModel() {
        // add indices on grid ?
        this.indices = null;
        this.value = 0;
        this.candidates = new ValuesSet();
        this.isBase = false;
    }

    CellModel.revive = function (data) {
        var inst = new CellModel();
        var content = data.content;
        inst.value = content[0];
        inst.isBase = content[1] === 'b'

        var candidates = inst.isBase ? [] : content[1];
        for (var i = 0, len = candidates.length; i < len; i++) {
            inst.candidates.add(candidates[i]);
        }

        return inst;
    }

    CellModel.prototype.toJSON = function () {
        var content = [];
        content[0] = this.value;
        if (!this.isBase) {
            var candidates = [];
            for (var v = 0; v < 10; v++) {
                if (this.candidates.has[v])
                    candidates.push(v);
            }
            content[1] = candidates;
        }
        else
            content[1] = 'b';

        var js = {};
        js._t = 'CellModel';
        js.content = content

        return js;
    }

    CellModel.prototype.toggleCandidate = function (value) {
        if (this.isBase)
            return;

        if (this.candidates.has[value])
            this.candidates.remove(value);
        else
            this.candidates.add(value);

        if (this.value !== 0) {
            this.value = 0;
        }
    }

    CellModel.prototype.setValue = function (value) {
        this.value = value;
        this.candidates.clear();
    }

    CellModel.prototype.copy= function (otherCell) {
        this.value = otherCell.value;
        this.candidates.clear();
        this.candidates.addRange(otherCell.candidates.values);
    }

    CellModel.prototype.getHash = function () {
        return (this.isBase ? 1 : 0) + '|' + this.value + '|' + this.candidates.getMask();
    }

}(window));
